﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;
using MyFloridaHouse.libSvOpenDistricts.Criteria;
using MyFloridaHouse.libSvCommonUtilities;

namespace MyFloridaHouse.libSvOpenDistricts.MapShapes
{
    public partial class RegionLabel : INotifyPropertyChanged
    {
        public const string CriteriaSubscriberName = "RegionLabel";

        #region propertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region static properties
        private static RegionLabel _regionLabelInstance;
        public static RegionLabel regionLabelInstance
        {
            get { return _regionLabelInstance; }
        }
        #endregion

        #region public properties

        private bool _isRegionLabelOn = true;
        public bool isRegionLabelOn
        {
            get { return _isRegionLabelOn; }
            set
            {
                if (_isRegionLabelOn != value)
                {
                    _isRegionLabelOn = value;
                    NotifyPropertyChanged("isRegionLabelOn");
                    RaiseRegionLabelDataChanged();
                }
            }
        }

        //public int dataItemDictionaryID
        //{
        //    get
        //    {
        //        if ((selectedDatasetCriteriaIndex >= 0) & (selectedDatasetCriteriaIndex <= datasetCriteriaList.Count))
        //        {
        //            return datasetCriteriaList[selectedDatasetCriteriaIndex].dictionaryList[0].dictionaryID;
        //        }
        //        else
        //        {
        //            return -1;
        //        }
        //    }
        //    set { }
        //}

        //public string dataItemHeader
        //{
        //    get
        //    {
        //        if ((selectedDatasetCriteriaIndex >= 0) & (selectedDatasetCriteriaIndex <= datasetCriteriaList.Count))
        //        {
        //            return datasetCriteriaList[selectedDatasetCriteriaIndex].criteriaHeader;
        //        }
        //        else
        //        {
        //            return "";
        //        }
        //    }
        //    set { }
        //}


        private string _criteriaDatasetName = "";
        public string criteriaDatasetName
        {
            get { return _criteriaDatasetName; }
            set
            {
                if (_criteriaDatasetName != value)
                {
                    _criteriaDatasetName = value;
                    NotifyPropertyChanged("criteriaDatasetName");
                    RaiseRegionLabelDataChanged();
                }
            }
        }

        private ObservableCollection<CriteriaDatasetDTO> _datasetCritieraList;
        public ObservableCollection<CriteriaDatasetDTO> datasetCriteriaList
        {
            get { return _datasetCritieraList; }
            set
            {
                if (_datasetCritieraList != value)
                {
                    _datasetCritieraList = value;
                    NotifyPropertyChanged("datasetCriteriaList");
                    // don't need to RaiseRegionLabelChanged because it is not a primary driver
                }
            }
        }


        private int _selectedDatasetCriteriaIndex = -1;
        public int selectedDatasetCriteriaIndex
        {
            get { return _selectedDatasetCriteriaIndex; }
            set
            {
                // turn off old selection
                if ((selectedDatasetCriteriaIndex >= 0) & (selectedDatasetCriteriaIndex <= datasetCriteriaList.Count))
                {
                    datasetCriteriaList[selectedDatasetCriteriaIndex].isSelected = false;
                }

                _selectedDatasetCriteriaIndex = value;

                // turn on new selection
                if ((selectedDatasetCriteriaIndex >= 0) & (selectedDatasetCriteriaIndex <= datasetCriteriaList.Count))
                {
                    datasetCriteriaList[selectedDatasetCriteriaIndex].isSelected = true;
                }
                CriteriaDatasetManager.UpdateSubscriberDisplayCriteriaIndex(CriteriaSubscriberName, selectedDatasetCriteriaIndex);

                NotifyPropertyChanged("selectedDatasetCriteriaIndex");
                RaiseRegionLabelDataChanged();
            }
        }

        #endregion


		/// <summary>
		/// Initializes a new instance of the <see cref="RegionLabel"/> class.
		/// Constructor: When the regionLabel object is created, store a reference to it in the static class.
		/// </summary>
        public RegionLabel()
        {
            // when the regionLabel object is created, store a reference to it in the static class
            // That way anything that needs to access the instance methods can do so without passing around references all over the place
            _regionLabelInstance = this;
            //RegionLabel.InitializeEventListeners();

            LoadCriteriaDataset(Constants.DefaultCriteriaDatasetName);

        }

        public void LoadCriteriaDataset(string newCriteriaDatasetName)
        {
            if (this.criteriaDatasetName != newCriteriaDatasetName)
            {
                this.criteriaDatasetName = newCriteriaDatasetName;
                this.datasetCriteriaList = CriteriaDatasetManager.Subscribe(newCriteriaDatasetName, RegionLabel.CriteriaSubscriberName);
            }
        }

        public UIElement GetRegionLabelShape(List<RegionDataDTO> regionDataList)
        {
            Border labelBorder = new Border();
            labelBorder.Background = new SolidColorBrush(Colors.Transparent);
            labelBorder.CornerRadius = new CornerRadius(3);
            labelBorder.Opacity = .8;

            Grid labelGrid = new Grid();
            labelBorder.Child = labelGrid;
            int rowCount = 1;
            for (int i=0; i<rowCount; i++)
            {
                RowDefinition rd=new RowDefinition();
                labelGrid.RowDefinitions.Add(rd);
            }            
            int colCount = 1;
            for (int i=0; i<colCount; i++)
            {
                ColumnDefinition cd=new ColumnDefinition();
                labelGrid.ColumnDefinitions.Add(cd);
            }

            if (isRegionLabelOn)
            {
                if (regionDataList != null)
                {
                    if ((selectedDatasetCriteriaIndex >= 0) & (selectedDatasetCriteriaIndex <= datasetCriteriaList.Count))
                    {
                        // find the data value
                        int regionComputedDataValue = 0;

                        TextBlock frontText;
                        if (CriteriaDataset.EvaluateCriteria(regionDataList, datasetCriteriaList[selectedDatasetCriteriaIndex], false, out regionComputedDataValue))
                        {
                            // now create a label for the map region
                            frontText = MakeText(regionComputedDataValue.ToString(), FontWeights.ExtraBold, Colors.Cyan);
                        }
                        else
                        {
                            // there was a problem with the evaluation
                            frontText = MakeText("", FontWeights.Normal, Colors.Yellow);
                        }
                        frontText.SetValue(Grid.RowProperty, 0);
                        frontText.SetValue(Grid.ColumnProperty, 0);


                        labelGrid.Children.Add(frontText);
                    }
                
                }
            }
            return labelBorder;
        }

        private TextBlock MakeText(string newText, FontWeight fontWeight, Color color)
        {
            TextBlock tb = new TextBlock();
            if (newText.Length == 0)
            {
                tb.Text = "";
            }
            else
            {
                tb.Text = newText;
            }
            tb.FontFamily = new FontFamily("Times New Roman");
            tb.FontSize = 12.0;
            tb.HorizontalAlignment = HorizontalAlignment.Center;
            tb.VerticalAlignment = VerticalAlignment.Center;
            tb.Foreground = new SolidColorBrush(color);
            tb.FontWeight = new FontWeight();
            tb.FontWeight = fontWeight;
            tb.Padding = new Thickness(2);



            //tb.Effect = new System.Windows.Media.Effects.DropShadowEffect();

            return tb;
        }

    }
}
